﻿//-----------------------------------------------------------------------
// <copyright file="HelpDocumentationCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//-----------------------------------------------------------------------

namespace ChatBox.UI.Windows.Main    
{
    using System;
    using System.Diagnostics;

    using ChatBox.DomainObjects;
    using ChatBox.Interfaces;
    using ChatBox.UI.Commands;

    /// <summary>
    /// Handler for help commands
    /// </summary>
    public class HelpDocumentationCommand : GenericCommandBase<MainWindowViewModel>
    {
        /// <summary>
        /// Log destination
        /// </summary>
        private ILogger log;

        /// <summary>
        /// Initializes a new instance of the HelpDocumentationCommand class
        /// </summary>
        /// <param name="log">the error log</param>
        public HelpDocumentationCommand(ILogger log)
        {
            this.log = log;
        }

        /// <summary>
        /// Execute the help about command
        /// </summary>
        /// <param name="value">the command data</param>
        public override void ExecuteOnValue(MainWindowViewModel value)
        {
            this.NavigateToUri(value, VersionConstants.DocumentationPage);
        }

        /// <summary>
        /// navigate to the Uri
        /// </summary>
        /// <param name="viewModel">the viewModel to update</param>
        /// <param name="navigateUri">the url to navigate to</param>
        private void NavigateToUri(MainWindowViewModel viewModel, string navigateUri)
        {
            try
            {
                Process.Start(new ProcessStartInfo(navigateUri));
            }
            catch (Exception ex)
            {
                // the browser that was launched, is wedged. display the failure
                string message = "Click on link " + navigateUri + " failed";
                viewModel.StatusText = message + ": " + ex.Message;

                this.log.LogException(message, ex);
            }
        }
    }
}
